package com.hmyd.hmyd_android.adapter;

import java.util.List;

import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.adapter.CouponListAdapter.ViewHolder;
import com.hmyd.hmyd_android.dto.AddInterests;
import com.hmyd.hmyd_android.dto.Coupon;
import com.hmyd.hmyd_android.ui.MainActivity;
import com.hmyd.hmyd_android.utils.Utils;

import android.content.Context;
import android.content.Intent;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AddInterestsTicketAdapter extends BaseAdapter {

	private Context mContext;
	private List<AddInterests> mPlist;
	private LayoutInflater mInflater;
	private String mType;

	public AddInterestsTicketAdapter(Context context, List<AddInterests> plist, String type){
		super();
		
		this.mContext = context;
		this.mPlist = plist;
		mInflater=LayoutInflater.from(context);
		mType = type;
	}
	
	@Override
	public int getCount() {
		return mPlist != null ? mPlist.size() : 0;
	}

	@Override
	public AddInterests getItem(int position) {
		return mPlist != null ? mPlist.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(mPlist==null || mPlist.get(position)==null){
			return convertView;
		}
		
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_add_interests_ticket, null);
			holder = new ViewHolder();
			holder.tv_plus_sign = (TextView)convertView.findViewById(R.id.tv_plus_sign);
			holder.extraRate = (TextView)convertView.findViewById(R.id.tv_ticket);
			holder.tv_percentage = (TextView)convertView.findViewById(R.id.tv_percentage);
			holder.couponName = (TextView)convertView.findViewById(R.id.rank_ticket);
			holder.deadline = (TextView)convertView.findViewById(R.id.deadline);
			holder.applyToItemType = (TextView)convertView.findViewById(R.id.suit_type);
			holder.rank_icon = (ImageView)convertView.findViewById(R.id.rank_icon);
			holder.use_btn = (TextView) convertView.findViewById(R.id.use_btn);
			holder.add_interestes_ticket_left = (RelativeLayout) convertView.findViewById(R.id.add_interestes_ticket_left);
			holder.add_interestes_ticket_right = (RelativeLayout) convertView.findViewById(R.id.add_interestes_ticket_right);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		AddInterests addInterests = getItem(position);
		if(addInterests!=null){
			holder.extraRate.setText(Utils.changePointToPercent(addInterests.extraRate));
			holder.couponName.setText(addInterests.couponName);
			holder.applyToItemType.setText("使用限制："+addInterests.applyToItemType);
			
			// 判断 nominalValue 长度显示字体大小
			int length = holder.extraRate.length();
			if (length >= 3) {
				holder.extraRate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 42);
			} else {
				holder.extraRate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 57);
			}
			
			if("usable".equals(mType)){//可使用
				holder.deadline.setText("有效期："+Utils.getDateString(addInterests.effectTime)+"至"+Utils.getDateString(addInterests.expTime));
				holder.applyToItemType.setText("适用标的："+addInterests.applyToItemType);
				holder.use_btn.setEnabled(true);
				/*
				 * 根据加息券类型改变“立即使用”字体的颜色
				 * 右上角加息券图标
				 * 边框颜色，左右同时改变
				 */
				if(addInterests.couponName.equals("青铜券")){
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_t1));
					holder.use_btn.setTextColor(mContext.getResources().getColor(R.color.color_one));
					holder.add_interestes_ticket_left.setBackgroundResource(R.drawable.jia_img_left_1);
					holder.add_interestes_ticket_right.setBackgroundResource(R.drawable.jia_img_right_1);
				} else if(addInterests.couponName.equals("白银券")){
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_y2));
					holder.use_btn.setTextColor(mContext.getResources().getColor(R.color.color_two));
					holder.add_interestes_ticket_left.setBackgroundResource(R.drawable.jia_img_left_2);
					holder.add_interestes_ticket_right.setBackgroundResource(R.drawable.jia_img_right_2);
				} else if(addInterests.couponName.equals("黄金券")){
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_j3));
					holder.use_btn.setTextColor(mContext.getResources().getColor(R.color.color_three));
					holder.add_interestes_ticket_left.setBackgroundResource(R.drawable.jia_img_left_3);
					holder.add_interestes_ticket_right.setBackgroundResource(R.drawable.jia_img_right_3);
				} else if(addInterests.couponName.equals("钻石券")){
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_z4));
					holder.use_btn.setTextColor(mContext.getResources().getColor(R.color.color_four));
					holder.add_interestes_ticket_left.setBackgroundResource(R.drawable.jia_img_left_4);
					holder.add_interestes_ticket_right.setBackgroundResource(R.drawable.jia_img_right_4);
				} else if(addInterests.couponName.equals("VIP券")){
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_v5));
					holder.use_btn.setTextColor(mContext.getResources().getColor(R.color.color_vip));
					holder.add_interestes_ticket_left.setBackgroundResource(R.drawable.jia_img_left_5);
					holder.add_interestes_ticket_right.setBackgroundResource(R.drawable.jia_img_right_5);
				}
			}else if("used".equals(mType)){//已经使用
				holder.deadline.setText("使用时间："+Utils.getDateString(addInterests.usedTime));
				holder.applyToItemType.setText("投资项目："+addInterests.itemName);
				holder.use_btn.setEnabled(false);
				if (addInterests.couponName.equals("青铜券")) {
					holder.use_btn.setTextColor(mContext.getResources().getColor(R.color.color_one));
					holder.use_btn.setEnabled(false);
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_t1));
					holder.add_interestes_ticket_left.setBackgroundResource(R.drawable.jia_img_left_1);
					holder.add_interestes_ticket_right.setBackgroundResource(R.drawable.jia_img_right_1);
				} else if(addInterests.couponName.equals("白银券")){
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_y2));
					holder.use_btn.setEnabled(false);
					holder.use_btn.setTextColor(mContext.getResources().getColor(R.color.color_two));
					holder.add_interestes_ticket_left.setBackgroundResource(R.drawable.jia_img_left_2);
					holder.add_interestes_ticket_right.setBackgroundResource(R.drawable.jia_img_right_2);
				} else if(addInterests.couponName.equals("黄金券")){
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_j3));
					holder.use_btn.setTextColor(mContext.getResources().getColor(R.color.color_three));
					holder.use_btn.setEnabled(false);
					holder.add_interestes_ticket_left.setBackgroundResource(R.drawable.jia_img_left_3);
					holder.add_interestes_ticket_right.setBackgroundResource(R.drawable.jia_img_right_3);
				} else if(addInterests.couponName.equals("钻石券")){
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_z4));
					holder.use_btn.setTextColor(mContext.getResources().getColor(R.color.color_four));
					holder.use_btn.setEnabled(false);
					holder.add_interestes_ticket_left.setBackgroundResource(R.drawable.jia_img_left_4);
					holder.add_interestes_ticket_right.setBackgroundResource(R.drawable.jia_img_right_4);
				} else if(addInterests.couponName.equals("VIP券")){
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_v5));
					holder.use_btn.setTextColor(mContext.getResources().getColor(R.color.color_vip));
					holder.use_btn.setEnabled(false);
					holder.add_interestes_ticket_left.setBackgroundResource(R.drawable.jia_img_left_5);
					holder.add_interestes_ticket_right.setBackgroundResource(R.drawable.jia_img_right_5);
				}
			}else if("expired".equals(mType)){//过期
				holder.deadline.setText("过期时间："+Utils.getDateString(addInterests.expTime));
				holder.deadline.setTextColor(mContext.getResources().getColor(R.color.color_gray));
				holder.applyToItemType.setText("适用标的："+addInterests.applyToItemType);
				holder.applyToItemType.setTextColor(mContext.getResources().getColor(R.color.color_gray));
				holder.use_btn.setEnabled(false);
				holder.use_btn.setBackgroundResource(R.drawable.white_border_gray_in_shape);
				holder.add_interestes_ticket_left.setBackgroundResource(R.drawable.jia_img_left_6);
				holder.add_interestes_ticket_right.setBackgroundResource(R.drawable.jia_img_right_6);
				holder.couponName.setTextColor(mContext.getResources().getColor(R.color.color_gray));
				if (addInterests.couponName.equals("青铜券") || addInterests.couponName.equals("白银券") || addInterests.couponName.equals("黄金券")) {
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_1_hui));
				} else if (addInterests.couponName.equals("钻石券")) {
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_2_hui));
				} else if (addInterests.couponName.equals("VIP券")) {
					holder.rank_icon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.jia_icon_3_hui));
				}
			}
			
			holder.use_btn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mContext,MainActivity.class);
					intent.putExtra("key", "investment");
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(intent);
					AppBasic.getInstance().finishCurrentActivity();
				}
			});
			
		}
		
		return convertView;
	}
	
	class ViewHolder{
		public TextView tv_plus_sign;	//券息+号
		public TextView extraRate;	//券息
		public TextView tv_percentage;	//券息百分号
		public TextView couponName;	//券名
		public TextView deadline;	//有效期
		public TextView applyToItemType;	//适用类型
		public ImageView rank_icon;	//券图标
		public TextView use_btn;
		
		public RelativeLayout add_interestes_ticket_left;
		public RelativeLayout add_interestes_ticket_right;
	}

}
